package biz.datalk.industrialland.admin.modules.online.config.util;

import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformFieldDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformField;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.vo.DynformFieldVo;
import biz.datalk.industrialland.common.def.enums.DBYNEnum;
import biz.datalk.industrialland.common.exception.ServiceException;
import biz.datalk.industrialland.common.util.FieldUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.*;

/**
 * 字段工具类
 *
 * @author tarofang@163.com
 * @date 2019-12-30
 *
 */
public class DynformFieldUtil {

    public static List<DynformFieldVo> group(List<DynformFieldVo> list) {
        LinkedHashMap<Integer, List<DynformFieldVo>> groupMap = new LinkedHashMap<>();
        List<DynformFieldVo> groupItem;
        int groupid = 0;
        for (DynformFieldVo item : list) {
            groupid = item.getGroupId();
            groupItem = groupMap.get(groupid);
            if (groupItem == null) {
                groupItem = new ArrayList<>();
                groupItem.add(item);
                groupMap.put(groupid, groupItem);
            } else {
                groupItem.add(item);
            }
        }

        List<DynformFieldVo> groupList = new ArrayList<>();
        DynformFieldVo group;
        for (Map.Entry<Integer, List<DynformFieldVo>> entry : groupMap.entrySet()) {
            groupid = entry.getKey();
            groupItem = entry.getValue();

            group = new DynformFieldVo();
            group.setGroupId(groupid);
            group.setGroupName(groupItem.get(0).getGroupName());
            group.setChildren(groupItem);

            groupList.add(group);
        }

        return groupList;
    }

    public static List<DynformFieldDTO> getPageShowFields(List<DynformFieldDTO> fields, boolean includePk) {
        List<DynformFieldDTO> resList = new ArrayList<>();
        for (DynformFieldDTO existsField : fields) {
            if (includePk && StringUtils.equals(existsField.getFieldIsPk(), DBYNEnum.Y.name())) {
                resList.add(existsField);
                continue;
            }
            if (StringUtils.equals(existsField.getPageIsShow(), DBYNEnum.Y.name())) {
                resList.add(existsField);
            }
        }
        return resList;
    }

    public static List<DynformFieldDTO> getQueryFields(List<DynformFieldDTO> fields) {
        List<DynformFieldDTO> resList = new ArrayList<>();
        for (DynformFieldDTO existsField : fields) {
            if (StringUtils.equals(existsField.getPageIsQuery(), DBYNEnum.Y.name())) {
                resList.add(existsField);
            }
        }
        return resList;
    }

    public static List<DynformFieldDTO> getPageShowListFields(List<DynformFieldDTO> existsFields) {
        List<DynformFieldDTO> resList = new ArrayList<>();
        for (DynformFieldDTO existsField : existsFields) {
            if (StringUtils.equals(existsField.getFieldIsPk(), DBYNEnum.Y.name())) {
                resList.add(existsField);
                continue;
            }
            if (StringUtils.equals(existsField.getPageIsShowList(), DBYNEnum.Y.name())) {
                resList.add(existsField);
            }
        }
        return resList;
    }

    public static Object convertVal(DynformField field, String val) {
        Object obj = null;
        switch (field.getFieldType()) {
            case "varchar":
            case "text":
            case "mediumtext":
                obj = val;
                break;
            case "datetime":
            case "timestamp":
                String pageShowType = field.getPageShowType();
                try {
                    if (StringUtils.equals(pageShowType, "date")) {
                        obj = DateFormatUtils.format(DateUtils.parseDate(val, "yyyy-MM-dd"), "yyyy-MM-dd 00:00:00");
                    } else if (StringUtils.equals(pageShowType, "datetime")) {
                        obj = DateFormatUtils.format(DateUtils.parseDate(val, "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
                    } else {
                        throw new ServiceException("不支持的日期格式");
                    }
                } catch (Exception ex) {
                    throw new ServiceException("日期格式解析错误");
                }
                break;
            case "int":
                obj = Integer.valueOf(val);
                break;
            case "double":
                obj = Double.valueOf(val);
                break;
            default:
                break;
        }
        return obj;

    }

    public static Map<String, DynformFieldDTO> fieldNameHumpKeyMap(List<DynformFieldDTO> fields, boolean orderly) {
        Map<String, DynformFieldDTO> map = orderly ? new LinkedHashMap<>() : new HashMap<>(32);
        if (fields == null || fields.isEmpty()) {
            return map;
        }
        for (DynformFieldDTO field : fields) {
            map.put(FieldUtil.underline2Hump(field.getFieldName()), field);
        }
        return map;
    }
}
